package com.boyuan.huijiaoyu;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMap.OnMarkerClickListener;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.magicsoft.app.entity.MapListResp;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.MapService;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;

public class MapDetailActivity extends Activity implements OnClickListener,
		OnMarkerClickListener {

	private MapView mMapView;
	private AMap aMap;
	private UiSettings mUiSettings;
	private int mapType = 0;
	private TextView mTv_title;
	private TextView mBtnBack;
	private MapService mapService;
	List<MapListResp> mapListResps = new ArrayList<MapListResp>();
	List<MapListResp> hierarchyList = new ArrayList<MapListResp>();
	private String tag = "MapDetailActivity";
	private String uid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_detail);
		// 获取用户个人uid
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		uid = sp.getString("id", "");

		mMapView = (MapView) findViewById(R.id.bmapsView);
		mMapView.onCreate(savedInstanceState);// 此方法必须重写
		initUI();
		initMap();
		getMapList();
	}

	// 获取地图气泡数据
	private void getMapList() {
		if (mapService == null) {
			mapService = new MapService(this);
		}

		String cid = getmapId();
		Log.e(tag, "maplist Id =" + cid);
		mapService.getMapList(cid, uid,
				new GetOneRecordListener<List<MapListResp>>() {

					@Override
					public void onFinish(List<MapListResp> data) {
						Log.e(tag, "data =" + data.toString());
						mapListResps.clear();
						if (data != null) {
							mapListResps.addAll(data);
						}
						addMarker(data);
					}

					@Override
					public void onFailed(String Message) {
						ToastHelper.showMsg(MapDetailActivity.this, Message,
								false);
					}
				});

	}

	// 根据层级数获取Id
	private String getmapId() {

		String id = "";
		MapListResp mapListResp = hierarchyList.get(mapType);
		id = mapListResp.getId();

		return id;
	}

	// 添加气泡
	private void addMarker(List<MapListResp> datas) {
		Gson gson = new Gson();
		if (aMap != null) {
			aMap.clear();
			aMap.invalidate();
		}
		for (MapListResp mapListResp : datas) {
			String lat = mapListResp.getLat();
			String lng = mapListResp.getLng();
			LatLng latLng = new LatLng(Double.parseDouble(lat),
					Double.valueOf(lng));
			aMap.addMarker(new MarkerOptions()
					.anchor(0.5f, 0.5f)
					.position(latLng)
					.title(mapListResp.getName())
					.snippet(gson.toJson(mapListResp))
					.icon(BitmapDescriptorFactory
							.fromView(getMapPaoPaoView(mapListResp.getName())))
					.draggable(true));
		}

	}

	private View getMapPaoPaoView(String content) {

		View view = LayoutInflater.from(MapDetailActivity.this).inflate(
				R.layout.map_paopao2, null);
		TextView textView = (TextView) view.findViewById(R.id.popview_title);
		textView.setText(content);

		return view;
	}

	// 设置地图中心点，视距以及标题
	private void setMapCenter(double lat, double lon) {
		int zoom;
		switch (mapType) {
		case 0:
			zoom = 4;
			break;
		case 1:
			zoom = 8;
			break;
		case 2:
			zoom = 14;
			break;
		default:
			zoom = 18;
			break;
		}

		LatLng marker1 = new LatLng(lat, lon); // 设置中心点
		aMap.moveCamera(CameraUpdateFactory.changeLatLng(marker1));
		aMap.moveCamera(CameraUpdateFactory.zoomTo(zoom));
		// 标题显示层级名字组合
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < hierarchyList.size(); i++) {
			sb.append(hierarchyList.get(i).getName() + "-");
		}
		String title = sb.toString();
		String substring = title.substring(0, title.length() - 1);
		mTv_title.setText(substring);
	}

	private void initUI() {
		mBtnBack = (TextView) findViewById(R.id.btnBack);
		mBtnBack.setOnClickListener(this);
		mBtnBack.setVisibility(View.INVISIBLE);
		mTv_title = (TextView) findViewById(R.id.tv_title);
		findViewById(R.id.btnClose).setOnClickListener(this);
	}

	private void initMap() {
		if (aMap == null) {
			aMap = mMapView.getMap();
			mUiSettings = aMap.getUiSettings();

			mUiSettings.setZoomGesturesEnabled(true);
			mUiSettings.setScrollGesturesEnabled(true);
			mUiSettings.setZoomControlsEnabled(false);

			mUiSettings.setScaleControlsEnabled(true);
			// aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
			aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
			aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false

			mUiSettings.setMyLocationButtonEnabled(false); // 是否显示默认的定位按钮
			// addMarkers(getMapData(), 40.84706036, 114.82910156, 8);

			// aMap.setOnMapLoadedListener(this);
			// aMap.setOnMapClickListener(this);
			aMap.setOnMarkerClickListener(this);

			MapListResp mapListResp = new MapListResp();
			mapListResp.setId("0");
			mapListResp.setName("全国中心");
			mapListResp.setLat("35.6972813");
			mapListResp.setLng("104.238281");
			hierarchyList.add(0, mapListResp);

			setMapCenter(35.6972813, 104.238281);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBack:
			btn_back();
			break;
		case R.id.btnClose:
			finish();
			break;

		default:
			break;
		}

	}

	// 返回上一级
	private void btn_back() {
		if (mapType > 0) {
			hierarchyList.remove(mapType);
			mapType--;
			MapListResp mapListResp = hierarchyList
					.get(hierarchyList.size() - 1);
			double lat = Double.parseDouble(mapListResp.getLat());
			double lng = Double.parseDouble(mapListResp.getLng());
			if (aMap != null) {
				aMap.clear();
				aMap.invalidate();
			}
			getMapList();
			if (mapType == 0) {
				mBtnBack.setVisibility(View.INVISIBLE);
			}
			setMapCenter(lat, lng);
		} else {
			finish();
		}
	}

	// 气泡点击事件
	@Override
	public boolean onMarkerClick(Marker marker) {
		if (aMap != null) {
			aMap.clear();
			aMap.invalidate();
		}
		String snippet = marker.getSnippet();
		Gson gson = new Gson();
		Type type = new TypeToken<MapListResp>() {
		}.getType();
		MapListResp mapListResp = gson.fromJson(snippet, type);
		double lat = Double.parseDouble(mapListResp.getLat());
		double lon = Double.parseDouble(mapListResp.getLng());
		mapType++;
		// 将层级的气泡对象存入容器中
		if (mapType >= 0) {
			MapListResp hierarResp = new MapListResp();
			hierarResp.setId(mapListResp.getId());
			hierarResp.setName(mapListResp.getName());
			hierarResp.setLat(mapListResp.getLat());
			hierarResp.setLng(mapListResp.getLng());
			hierarResp.setParent(mapListResp.getParent());
			hierarResp.setType(mapListResp.getType());
			hierarchyList.add(mapType, hierarResp);
		}
		getMapList();
		mBtnBack.setVisibility(View.VISIBLE);
		setMapCenter(lat, lon);
		return true;
	}

}
